﻿Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_Action_Edit
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
    Dim level As String = -1
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "jqueryEnable", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "jqueryEnable", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            If Not Request.QueryString("Actionid").ToString.Equals("0") Then
                hidID.Value = Request.QueryString("Actionid")
                getFunction()
                ShowData()
            End If
        End If
    End Sub
    Protected Function checkExist() As Boolean
        Using data As New VSEntities
            Dim p = (From q In data.Actions Where q.Title = txtTitle.Text And q.FunctionId = ddlFunction.SelectedValue And q.ActionId <> hidID.Value Select q).FirstOrDefault
            If p Is Nothing Then
                Return True
            Else
                Return False
            End If
        End Using
    End Function
    Protected Sub getFunction()
        ddlFunction.Items.Clear()
        Using data As New VSEntities
            Dim p As List(Of SysFunction) = (From q In data.SysFunctions Where q.IsStatus = True Order By q.Sort, q.HrefName Ascending Select q).ToList
            RecursiveFillTree(p, 0)
            ddlFunction.Items.Insert(0, New ListItem("--- Chọn ---", "0"))
        End Using
    End Sub
    Private Sub RecursiveFillTree(ByVal dtParent As List(Of SysFunction), ByVal _parentID As Integer)

        Using data As New VSEntities
            Dim dv As List(Of SysFunction) = (From q In data.SysFunctions Where q.IsStatus = True Order By q.Sort, q.HrefName Ascending Select q).ToList
            Dim i As Integer

            If dv.Count > 0 Then
                For i = 0 To dv.Count - 1
                    Dim itm As New ListItem(Server.HtmlDecode(dv.Item(i).HrefName.ToString()), dv.Item(i).FunctionId.ToString())
                    ddlFunction.Items.Add(itm)
                    ' RecursiveFillTree(dtParent, Integer.Parse(dv.Item(i).FunctionId.ToString()))
                Next
            End If
        End Using


    End Sub

    Protected Sub ShowData()
        Using data As New VSEntities
            Dim p As Action = (From q In Data.Actions Where q.ActionId = hidID.Value Select q).SingleOrDefault
            If Not p Is Nothing Then
                txtTitle.Text = IIf(IsNothing(p.Title) = True, "", p.Title)
                ddlFunction.SelectedValue = IIf(IsNothing(p.FunctionId) = True, "0", p.FunctionId)
            End If
        End Using
    End Sub
#End Region
#Region "Event for control"
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If checkExist() Then
            Using data As New VSEntities
                Dim p As Action = (From q In data.Actions Where q.ActionId = hidID.Value).SingleOrDefault
                Try
                    p.Title = txtTitle.Text.Trim()
                    p.FunctionId = ddlFunction.SelectedValue
                    data.SaveChanges()
                    'Insert_App_Log("Insert Action:" & txtTitle.Text.Trim & "", Function_Name.Action, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Action/List.aspx';", Me.Page, True)
                Catch ex As Exception
                    log4net.Config.XmlConfigurator.Configure()
                    log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                    Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
                End Try
            End Using
        Else
            Excute_Javascript("Alertbox('Đã tồn tại thao tác này trên chức năng " + ddlFunction.SelectedItem.Text + ".');", Me.Page, True)

        End If

    End Sub
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        Response.Redirect("List.aspx")
    End Sub
    
#End Region
End Class
